<?php

namespace App\Http\Middleware;

use App\Models\Admin\Menu;
use App\Models\Admin\Role;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class checkIsLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (!auth()->guard('admin')->check()) {
            return ['status'=>-1,'msg'=>'请登录'];
        }

        if(auth()->guard('admin')->user()->username!=config('rbac.super')){

            $menu_ids = Role::where('id',auth()->guard('admin')->user()->role_id)->value('menus');
            $arr = Menu::whereIn('id',$menu_ids)->pluck('url','id')->toArray();
            $authMenu = array_filter($arr,function ($val){
                 if(!empty($val)){
                     return $val;
                 }
            });
            array_push($authMenu,'admin.');
            if(!in_array($request->route()->getName(),$authMenu)){
                 exit('无权限');
            }
        }
        return $next($request);
    }
}
